package com.alex.util.base64;

import java.io.UnsupportedEncodingException;

/**
 * @author alex
 */
public class Base64Utils {
    public Base64Utils() {
    }

    public static String getBASE64(String s) {
        if (s == null) {
            return null;
        } else {
            try {
                return getBASE64(s.getBytes("UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return null;
            }
        }
    }

    public static String getBASE64(byte[] b) {
        byte[] rb = org.apache.commons.codec.binary.Base64.encodeBase64(b);
        if (rb == null) {
            return null;
        } else {
            try {
                return new String(rb, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return null;
            }
        }
    }

    public static String getFromBASE64(String s) {
        if (s == null) {
            return null;
        } else {
            try {
                byte[] b = getBytesBASE64(s);
                return b == null ? null : new String(b, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return null;
            }
        }
    }

    public static byte[] getBytesBASE64(String s) {
        if (s == null) {
            return null;
        } else {
            try {
                byte[] b = org.apache.commons.codec.binary.Base64.decodeBase64(s.getBytes("UTF-8"));
                return b;
            } catch (Exception e) {
                return null;
            }
        }
    }
}